import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:my_app/app/core/utils/url_util.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';

class PhotoPreviewGalleryWidget extends StatefulWidget {
  const PhotoPreviewGalleryWidget({
    super.key,
    required this.images,
    this.index = 0,
  });

  ///画廊图片列表
  final List<ImageGalleryEntity> images;

  ///当前图片 下标
  final int index;

  @override
  State<PhotoPreviewGalleryWidget> createState() =>
      _PhotoPreviewGalleryWidgetState();
}

class _PhotoPreviewGalleryWidgetState extends State<PhotoPreviewGalleryWidget> {
  int curIndex = 0;
  late PageController _controller;

  @override
  void initState() {
    super.initState();
    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
    curIndex = widget.index;
    _controller = PageController(initialPage: curIndex);
  }

  @override
  void dispose() {
    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.black,
      child: GestureDetector(
        onTap: () => Get.back(),
        child: Stack(
          children: [
            buildPhotoViewGallery(),
            Positioned(
              top: 20.0,
              width: Get.width,
              height: 30.0,
              child: buildIndicator(),
            ),
            // Positioned(
            //   right: 15.0,
            //   top: 20.0,
            //   child: buildBackBtn(),
            // )
          ],
        ),
      ),
    );
  }

  ///构建返回按钮
  Widget buildBackBtn() {
    return IconButton(
        onPressed: () => Get.back(),
        icon: Icon(
          Icons.close,
          size: 30,
          color: Colors.white,
        ));
  }

  ///构建指示器
  Widget buildIndicator() {
    return Center(
      child: Text("${curIndex + 1}/${widget.images.length}",
          style: TextStyle(color: Colors.white, fontSize: 14)),
    );
  }

/*
  PhotoViewGalleryPageOptions getPhotoViewGalleryPageOptionByImageType(String url){
    if (isRemote(url)) {
      return PhotoViewGalleryPageOptions(
          imageProvider: CachedNetworkImageProvider(url),
          heroAttributes: PhotoViewHeroAttributes(tag: widget.heroTag));
    }
    if (isFileUrl(url)) {
      return PhotoViewGalleryPageOptions(
          imageProvider: FileImage(File(url)),
          heroAttributes: PhotoViewHeroAttributes(tag: widget.heroTag));
    }
    return PhotoViewGalleryPageOptions(
        imageProvider: AssetImage(url),
        heroAttributes: PhotoViewHeroAttributes(tag: widget.heroTag));
  }
*/

  ///构建画廊
  PhotoViewGallery buildPhotoViewGallery() {
    return PhotoViewGallery.builder(
      pageController: _controller,
      itemCount: widget.images.length,
      loadingBuilder: (context, event) => Center(
        child: Image.asset(
          'assets/images/loading.gif',
          width: 30.0,
        ),
      ),
      builder: (ctx, index) {
        if (isRemote(widget.images[index].url!)) {
          return PhotoViewGalleryPageOptions(
            imageProvider:
                CachedNetworkImageProvider(widget.images[index].url!),
            heroAttributes:
                PhotoViewHeroAttributes(tag: widget.images[index].tag!),
          );
        }
        if (isFileUrl(widget.images[index].url!)) {
          return PhotoViewGalleryPageOptions(
            imageProvider: FileImage(File(widget.images[index].url!)),
            heroAttributes:
                PhotoViewHeroAttributes(tag: widget.images[index].tag!),
          );
        }
        return PhotoViewGalleryPageOptions(
          imageProvider: AssetImage(widget.images[index].url!),
          heroAttributes:
              PhotoViewHeroAttributes(tag: widget.images[index].tag!),
        );
      },
      enableRotation: true,
      gaplessPlayback: true,
      onPageChanged: (index) {
        setState(() {
          curIndex = index;
        });
      },
    );
  }
}

class ImageGalleryEntity {
  String? url;

  String? tag;

  ImageGalleryEntity({this.url, this.tag});

  ImageGalleryEntity.fromJson(Map<String, dynamic> json) {
    url = json['url'];
    tag = json['tag'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['url'] = url;
    data['tag'] = tag;
    return data;
  }
}
